package com.etc.controller;

import com.etc.dto.ReGoods;
import com.etc.entity.Attribute;
import com.etc.entity.AttributeValue;
import com.etc.entity.CategoryListing;
import com.etc.entity.GoodsSku;
import com.etc.entity.PageImg;
import com.etc.service.ListingProService;
import com.etc.vo.AddAttrVo;
import com.etc.vo.ListingData;
import com.etc.vo.ListingImgVo;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.*;

@RequestMapping("/business")
@SessionAttributes("msg")
@Controller
public class ListingProducts {
    @Resource
    ListingProService service;



    @RequestMapping("/listingGoods")
    public ModelAndView listingGoods(MultipartFile[] PImg, MultipartFile[] PHtmlImg, ListingData goods, HttpServletRequest request){
        ModelAndView mv=new ModelAndView();

        //商品便签解析
        Map<String,String[]> tagValMap=new HashMap<>();
        Map<String,String[]> tagValNameMap=new HashMap<>();
        if(goods.getTag()!=null){
            for(int i=0;i<goods.getTag().length;i++){
                String[] tagval=request.getParameterValues(goods.getTag()[i]);
                String[] tagvalName=request.getParameterValues(goods.getTag()[i]+"valName");
                tagValMap.put(goods.getTag()[i],tagval);
                tagValNameMap.put(goods.getTag()[i],tagvalName);
            }
            goods.setTagValMapVal(tagValMap);
            goods.setTagValMapName(tagValNameMap);
        }
        //生成商品ID
//        Date now=new Date();
//        SimpleDateFormat sdf=new SimpleDateFormat("ddmmssSSS");
//        goods.setGoodsId(Integer.valueOf(sdf.format(now)));
//        ImgToVo(PImg, PHtmlImg, goods);

        try {
            service.listingProData(PImg,PHtmlImg,goods,request);
            mv.addObject("msg","上架成功");
        } catch (Exception e) {
            e.printStackTrace();
            mv.addObject("msg","上架失败");
        }finally {
            mv.setViewName("redirect: listing.do");
            return mv;
        }
    }

    private void ImgToVo(MultipartFile[] PImg, MultipartFile[] PHtmlImg, ListingData goods) {
        List<ListingImgVo> imgVos=new ArrayList<>();
        for (int i = 0; i < PImg.length; i++) {
            if(PImg[i].getOriginalFilename()!=""){
                ListingImgVo imgVo=new ListingImgVo();
                imgVo.setGoods_id(goods.getGoodsId());
                imgVo.setImg_classify(0);
                imgVo.setImg_url(goods.getGoodsId()+"/"+ UUID.randomUUID()+ PImg[i].getOriginalFilename());
                imgVo.setImg(PImg[i]);
                imgVos.add(imgVo);
            }
        }
        for (int i = 0; i < PHtmlImg.length; i++) {
            if(PHtmlImg[i].getOriginalFilename()!=""){
            ListingImgVo imgVo=new ListingImgVo();
            imgVo.setGoods_id(goods.getGoodsId());
            imgVo.setImg_classify(1);
            imgVo.setImg_url(goods.getGoodsId()+"/"+UUID.randomUUID()+ PHtmlImg[i].getOriginalFilename());
            imgVo.setImg(PHtmlImg[i]);
            imgVos.add(imgVo);
        }}
        goods.setImg(imgVos);
    }


    @RequestMapping("/listing")
    public String listing(){
        return "pro";
    }

    @RequestMapping("/queryCetegory")
    @ResponseBody
    public List<CategoryListing> queryCetegory(){
        return service.queryAllCetegory();
    }
    @RequestMapping("/queryAttr")
    @ResponseBody
    public List<Attribute> queryAttr(int type){
        return service.queryAttrByTyppe(type);
    }
    @RequestMapping("/queryAttrval")
    @ResponseBody
    public List<AttributeValue> queryAttrval(int attrId){
        return service.queryAttrValueByAttId(attrId);
    }


    @RequestMapping("/reviseGoods")
    public ModelAndView reviseGoods(int goodsid){
        ModelAndView mv=new ModelAndView();
        ReGoods goods=service.queryGoods(goodsid);
        mv.addObject("goods",goods);
        mv.setViewName("reviseGoods");
        return mv;
    }

    @RequestMapping(value = "/modifyGoods",produces = "text/html;charset=UTF-8")
    @ResponseBody
    public String modifyGoods(ListingData goods,String oldGoodsName) throws Exception {
        if(service.modify(goods,oldGoodsName)){
            return "修改成功";
        }else{
            return "修改失败";
        }
    }
    @RequestMapping(value = "/queryImgByGoodsId")
    @ResponseBody
    public List<PageImg> queryImgByGoodsId(int goodsId){
        return service.queryImgByGoodsId(goodsId);
    }



   // @RequestMapping(value = "/addOneImg.do",consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
   @RequestMapping(value = "/addOneImg")
   @ResponseBody
    public PageImg addOneImg(MultipartFile pimg,Integer classify,Integer goodsId, HttpServletRequest request) throws Exception {
        ListingImgVo imgVo=new ListingImgVo();
        imgVo.setGoods_id(goodsId);
        imgVo.setImg_classify(classify);
        imgVo.setImg_url(goodsId+"/"+UUID.randomUUID()+ pimg.getOriginalFilename());
        imgVo.setImg(pimg);
        imgVo=service.addImg(imgVo,request);
        PageImg img=new PageImg();
        img.setImg_url(imgVo.getImg_url());
        img.setImg_id(imgVo.getImg_id());
        img.setImg_classify(imgVo.getImg_classify());
        return img;
    }

    @RequestMapping(value = "/delImg",produces = "text/html;charset=UTF-8")
    @ResponseBody
    public String delImg(int imgid, HttpServletRequest request) throws Exception {
        if(service.delImg(imgid, request)){
            return "已删除";
        }else{
            return "删除失败";
        }
    }

    @RequestMapping("/querySku")
    @ResponseBody
    public List<GoodsSku> querySku(int goodsId){
        return service.querySkuByGoodsId(goodsId);
    }

    @RequestMapping(value = "/modifySku",produces = "text/html;charset=UTF-8")
    @ResponseBody
    public String modifySku(Integer skuid,Double price,Integer num){
        GoodsSku sku=new GoodsSku();
        sku.setSkuId(skuid);
        sku.setPric(price);
        sku.setNum(num);
        if(service.modifySku(sku)){
            return "修改成功";
        }else{
            return "修改失败";
        }
    }

    @RequestMapping(value = "/delSku",produces = "text/html;charset=UTF-8")
    @ResponseBody
    public String delSku(int skuid){
        if(service.delSku(skuid)){
            return "删除成功";
        }else{
            return "删除失败";
        }
    }

    @RequestMapping("/queryAttrByTyppe")
    @ResponseBody
    public List<Attribute> queryAttrByTyppe(int type){
        return service.queryAttrByTyppe(type);
    }

    @RequestMapping(value = "/addOneSku",produces = "text/html;charset=UTF-8")
    @ResponseBody
    public String addOneSku(GoodsSku goodsSku,String goodsName,HttpServletRequest request){
        String tagvalName="";
        String tagval="";
        String[] tag=request.getParameterValues("tag");
        List<Integer> tagid=new ArrayList<>();
        for (int i = 0; i < tag.length; i++) {
            tagid.add(Integer.valueOf(tag[i]));
        }
        Collections.sort(tagid);
        for(int i=0;i<tagid.size();i++){
            if(i!=0){tagval+=",";}
            tagvalName+=request.getParameter(tagid.get(i)+"valName");
            tagval+=tagid.get(i)+":"+request.getParameter(String.valueOf(tagid.get(i)));
        }
        tagvalName=goodsName+tagvalName;
        goodsSku.setSku_name(tagvalName);
        goodsSku.setSku_properties(tagval);
        if(service.addOneSku(goodsSku)){
            return "添加成功";
        }else{
            return "添加失败";
        }
    }

    @RequestMapping(value = "/addTag",produces = "text/html;charset=UTF-8")
    @ResponseBody
    public String addTag(String name,String val,int type) throws Exception {
        AddAttrVo attrVo=new AddAttrVo();
        attrVo.setAttr_value(val.split(" "));
        attrVo.setAttrName(name);
        attrVo.setType(type);
        System.out.println(attrVo);
        if(service.addTag(attrVo)){
            return "添加成功";
        }else{
            return "添加失败";
       }
    }
}
